package handlers

import (
	"1-12zk/api/request"
	"1-12zk/api/response"
	"1-12zk/dao"
	"1-12zk/models"
	"github.com/gin-gonic/gin"
)

func AddRoom(c *gin.Context) {
	UserId := c.GetUint("userId")
	var data request.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	dao.AddRoom(int(UserId))
	response.ResponseSuccess(c, "创建成功")
}

func Del(c *gin.Context) {
	var data request.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	room := models.Room{RoomUserId: data.RoomUserId}
	err = dao.Del(room)
	if err != nil {
		response.ResponseError(c, "删除失败")
		return
	}
	response.ResponseSuccess(c, "删除成功")
}

func AddRoomUser(c *gin.Context) {
	UserId := c.GetUint("userId")
	var data request.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if !dao.UserFind(UserId) {
		response.ResponseError(c, "用户不存在")
		return
	}
	if !dao.CreateRoomUser(data.RoomUserId, int(UserId)) {
		response.ResponseError(c, "添加失败")
		return
	}
	response.ResponseSuccess(c, "添加成功")
}

func Update(c *gin.Context) {
	var data request.Update
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	user := models.RoomUser{
		RoomUserId: data.RoomUserId,
		Type:       data.Type,
	}
	err = dao.Update(user)
	if err != nil {
		response.ResponseError(c, "修改失败")
		return
	}
	response.ResponseSuccess(c, "修改成功")
}
